﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System.Collections.Generic;
using System.Linq;
using Pipra.Math;

namespace Pipra.Uom
{
    /// <summary>
    /// Converts a value by applying an offset.
    /// </summary>
    public class OffsetValueConverter : ValueConverterBase
    {

        private readonly double _offset;

        /// <summary>
        /// Constructs an offset converter.
        /// </summary>
        /// <param name="type">The unit type.</param>
        /// <param name="from">The unit to convert from.</param>
        /// <param name="to">The unit to convert to.</param>
        /// <param name="offset">The amount to offset the value by.</param>
        public OffsetValueConverter(string type, IUom from, IUom to, double offset)
            : base(type, from, to)
        {
            _offset = offset;
        }

        /// <summary>
        /// The estimated complexity of the conversion.
        /// </summary>
        public override int OpCost
        {
            get { return 0 == _offset ? 0 : 1; }
        }

        /// <summary>
        /// Transforms a value.
        /// </summary>
        /// <param name="value">The value to get a transform from.</param>
        /// <returns>A transformed value.</returns>
        public override double TransformValue(double value)
        {
            return value + _offset;
        }

        /// <summary>
        /// Transforms a value.
        /// </summary>
        /// <param name="value">The value to transform.</param>
        public override void TransformValue(ref double value)
        {
            value += _offset;
        }

        /// <summary>
        /// Transforms a set of values.
        /// </summary>
        /// <param name="values">The values transform.</param>
        public override void TransformValues(double[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] += _offset;
            }
        }

        /// <summary>
        /// Transforms a set of values.
        /// </summary>
        /// <param name="values">The values to get a transform from.</param>
        /// <returns>A transformed set of values.</returns>
        public override IEnumerable<double> TransformValues(IEnumerable<double> values)
        {
            return values.Select(v => v + _offset);
        }

        public override bool HasInverse
        {
            get { return true; }
        }

        public override IValueConverter<double> GetInverse()
        {
            return new OffsetValueConverter(Type, To, From, -_offset);
        }
    }
}
